<template>
  <div class="pages sdInput">
    <input class="commonInput" v-model="value" :disabled="disableds" @blur="blur(value)" size="mini" type="text">
  </div>
</template>
<script>
export default {
  name: "sd-input",
  props: {
    val: "",
    disableds: "",
    deployType: {
      type: Number,
      default: 8
      // required: true
    }
  },
  data() {
    return {
      value: this.val,
      deploy: this.deployType
    };
  },
  watch: {
    val: function(val) {
      this.value = val;
    },
    value: function(val) {
      if (val != null) {
        val = val.toString();
        let arr = val.split(".");
        let num = "";
        arr[0] = arr[0].replace(/[^0-9]/gi, "").substring(0, this.deploy); // 去掉非数字&&保留8位
        if (arr[0] == "") {
          arr[0] = 0;
        }
        arr[0] = parseInt(arr[0]);
        if (arr.length > 1) {
          num = ".";
          num += arr[1].replace(/[^0-9]/gi, "").substring(0, 2);
        }
        this.value = arr[0] + num;
        this.$emit("update:val", this.value);
      }
    },
    deep: true
  },
  methods: {
    blur(val) {
      if (val.substr(val.length - 1, 1) == ".") {
        this.value = this.value.substr(0, this.value.length - 1);
      }
    }
  }
};
</script>
<style scoped>
.commonInput:disabled {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #c0c4cc;
  cursor: not-allowed;
}
</style>

